//
//  MainTabBarController.swift
//  微博(swfit)
//
//  Created by YLW on 15/9/21.
//  Copyright © 2015年 YLW. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        

        //添加自控制器
        addChildViewControllers()
        
        //添加按钮
        setupComposedButton()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composedButton)
    }
    
    private func setupComposedButton()
    {
        tabBar.addSubview(composedButton)
        
        let count = self.childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 1
        
        composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
    }
    
    private func addChildViewControllers()
    {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(ViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    
    private func addChildViewController(vc : UITableViewController , title : String , imageName : String) {
        
        let nav : UINavigationController = UINavigationController (rootViewController: vc)
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(nav)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private lazy var composedButton : UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
    
}
